package com.cocola.controller;

import com.cocola.pojo.Declaration;
import com.cocola.pojo.Education;
import com.cocola.pojo.Unit;
import com.cocola.service.DeclarationService;
import com.cocola.service.EducationSercice;
import com.cocola.service.UnitService;
import com.cocola.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Controller
@CrossOrigin
@RequestMapping("/unit")
public class UnitController {

    public static String UNITACCOUNT;
    public static String UNITNAME;
    public static String USERNAME;

    @Autowired
    private UnitService unitService;

    @Autowired
    private DeclarationService declarationService;

    @Autowired
    private EducationSercice educationSercice;


    @RequestMapping("/toLogin")
    public String toLogin() {
        return "login_unit";
    }

    /*
    **登陆
     */
    @RequestMapping("/doLogin")
    public String doLogin(String unitAccount, String password, Model model) {
        System.out.println("unitAccount:" + unitAccount);
        System.out.println("password:" + password);

        if(unitService.login(unitAccount, password)) {
            UNITACCOUNT = unitAccount;
            return "redirect:/unit/findDeclaration";

        }else {
            model.addAttribute("error", "用户名或密码错误");
        }

        return "login_unit";
    }

    /**
     * 注册1
     * register_unit 还未创建
     */
    public String toRegister() {
        return "register_unit";
    }

    /**
     * 注册2
     * register_unit 还未创建
     */
    public String doRegister(String unitAccount, String password, String passwordConfirm, String unitName,
                             String legalPerson, String unitLeval, String unitType, String unitIndustry,
                             String register, String address, String addressDetail, String mandator,
                             String businessLicense, String photo, String signature, String userIdcard, Model model) {

        if(!unitService.findOneUnit2(unitName).isEmpty()) {

            unitService.register(unitAccount, password, passwordConfirm, unitName, legalPerson,
                    unitLeval, unitType, unitIndustry, register, address, addressDetail,
                    mandator, businessLicense, photo, signature, userIdcard);

            return "login_unit";
        }else {
            model.addAttribute("error", "该用户已经存在");
        }

        return "register_unit";
    }


    /*
    **unit首页
     */
    @RequestMapping("/findDeclaration")
    public String findDeclaration(Model model) {

        try {
            if(UNITACCOUNT != null) {

                List<Unit> unit = unitService.findOneUnit(UNITACCOUNT);
                String unitName = unit.get(0).getUnitName();    //注册时 unitName 必填，所以这不判断是否为空了
                UNITNAME = unitName;

                List<Declaration> declarationList = unitService.findDeclaration(unitName);

                if(!declarationList.isEmpty()) {
                    USERNAME = declarationList.get(0).getUserName();
                }

                model.addAttribute("declarationList", declarationList);
                return "apply9";

            }else {
                model.addAttribute("error", "请登录");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "login_unit";
    }

    /*
    **查看申请详情
     */
    @RequestMapping("/detail")
    public String detail(Model model) {
        try {
            if(UNITACCOUNT != null) {

                //select 申请者的申请详情和教育信息
                List<Declaration> declarationList = declarationService.findOneDeclaration(USERNAME);
                List<Education> educationList = educationSercice.finOneEducation(USERNAME);

                model.addAttribute("declarationList", declarationList);
                model.addAttribute("educationList", educationList);

                return "apply10";

            }else {
                model.addAttribute("error", "请登陆");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return "apply9";

    }


    @RequestMapping("toUpdate")
    public String toUpdate(Model model) {

        if(UNITACCOUNT != null) {
            model.addAttribute("unitName", UNITNAME);
            return "update2";

        }else {
            model.addAttribute("error", "请登录");
        }

        return "login_unit";
    }

    /*
    **审核处理
     */
    @RequestMapping("/updateStatus")
    public String updateStatus(String status, Model model) {

        if(UNITACCOUNT != null) {
            List<Unit> unit = unitService.findOneUnit(UNITACCOUNT);
            unitService.updateStatus(status, UNITNAME);

            System.out.println("unitName:" + unit.get(0).getUnitName());
            System.out.println("updata status done ==================>");

            return "redirect:/unit/findDeclaration";

        }else {
            model.addAttribute("error", "请登录");
        }

        return "login_unit";
    }



}
